var _variables = require("./variables"), _eventEmitter = _interopRequireDefault(require("../../utils/event-emitter"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _toConsumableArray(e) {
    return _arrayWithoutHoles(e) || _iterableToArray(e) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(e) {
    if (Symbol.iterator in Object(e) || "[object Arguments]" === Object.prototype.toString.call(e)) return Array.from(e);
}

function _arrayWithoutHoles(e) {
    if (Array.isArray(e)) {
        for (var t = 0, a = new Array(e.length); t < e.length; t++) a[t] = e[t];
        return a;
    }
}

function _objectSpread(t) {
    for (var e = 1; e < arguments.length; e++) {
        var a = null != arguments[e] ? arguments[e] : {}, n = Object.keys(a);
        "function" == typeof Object.getOwnPropertySymbols && (n = n.concat(Object.getOwnPropertySymbols(a).filter(function(e) {
            return Object.getOwnPropertyDescriptor(a, e).enumerable;
        }))), n.forEach(function(e) {
            _defineProperty(t, e, a[e]);
        });
    }
    return t;
}

function _defineProperty(e, t, a) {
    return t in e ? Object.defineProperty(e, t, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = a, e;
}

Component({
    options: {
        multipleSlots: !0
    },
    properties: {
        appName: {
            type: String,
            value: "card"
        },
        remarkHint: {
            type: String,
            value: "让你的微信好友更了解你"
        },
        type: {
            type: String,
            value: _variables.TYPES.NORMAL_VERIFY
        },
        data: {
            type: Object,
            value: {},
            observer: function(e, t) {
                this.setData({
                    form: _objectSpread({}, this.data.form, e)
                });
            }
        },
        fields: {
            type: Array,
            value: [ [ "user" ], [ "corpname", "position", "email" ], [ "remark" ] ]
        },
        validFilds: {
            type: Array,
            value: [ {
                keys: "realname",
                tip: "请输入名字"
            }, {
                keys: "corpname",
                tip: "请输入公司"
            }, {
                keys: "position",
                tip: "请输入职务"
            } ]
        },
        searchOrient: {
            type: String,
            value: _variables.ORIENT.BOTTOM
        },
        isVerifyCode: {
            type: Boolean,
            value: !0
        },
        isShownSendBtn: {
            type: Boolean,
            value: !0
        }
    },
    data: {
        TYPES: _variables.TYPES,
        form: {}
    },
    ready: function() {
        this.mobile = this.selectComponent("#mobile"), this.$events = [ [ "card.image.cropper", this.changeAvatar.bind(this) ] ], 
        this.$events.forEach(function(e) {
            return _eventEmitter.default.addListener.apply(_eventEmitter.default, _toConsumableArray(e));
        });
    },
    detached: function() {
        this.$events && this.$events.forEach(function(e) {
            return _eventEmitter.default.removeListener.apply(_eventEmitter.default, _toConsumableArray(e));
        });
    },
    methods: {
        selectAvatar: function() {
            wx.chooseImage({
                count: 1,
                sizeType: [ "original" ],
                success: function(e) {
                    var t = e.tempFilePaths;
                    wx.navigateTo({
                        url: "/pages/card/cropper/cropper?path=".concat(t[0], "&name=avatar")
                    });
                }
            });
        },
        changeAvatar: function(e) {
            this.setData({
                "form.avatar": e
            }), this.$eventEmit("input", {
                name: "avatar",
                value: e
            });
        },
        onInput: function(e) {
            var t = e.detail, a = t.name, n = t.value;
            this.setData(_defineProperty({}, "form.".concat(a), n)), this.$eventEmit("input", {
                name: a,
                value: n
            });
        },
        onTextareaInput: function(e) {
            var t = e.currentTarget.dataset.name, a = e.detail.value;
            this.onInput({
                detail: {
                    name: t,
                    value: a
                }
            });
        },
        onFocusTextarea: function() {
            var e = this;
            this.setData({
                textareaShown: !0
            }), setTimeout(function() {
                e.setData({
                    textareaFocus: !0
                });
            }, 200);
        },
        onBlurTextarea: function() {
            this.setData({
                textareaShown: !1,
                textareaFocus: !1
            });
        },
        onSelect: function(e) {
            var t = e.detail, a = t.name, n = t.value;
            this.setData(_defineProperty({}, "form.".concat(a), n)), this.$eventEmit("input", {
                name: a,
                value: n
            });
        },
        valideForm: function(t) {
            return this.data.validFilds.filter(function(e) {
                return !t["".concat(e.keys)];
            }).map(function(e) {
                return e.tip;
            })[0];
        },
        getFormData: function() {
            var e = this.data.form, t = this.mobile.getFormData();
            if (t) {
                Object.assign(e, t);
                var a = this.valideForm(e);
                return a ? this.$eventEmit("fail", {
                    value: a
                }) : e;
            }
        },
        onSearchOrient: function(e) {
            var n = this, r = e.detail.name, i = this.data.searchOrient === _variables.ORIENT.BOTTOM;
            i && wx.pageScrollTo({
                scrollTop: 0,
                duration: 0
            });
            var t = this.createSelectorQuery();
            t.select("#".concat(r)).boundingClientRect(), t.selectViewport().scrollOffset(), 
            t.exec(function(e) {
                var t, a = e[0].top;
                t = i ? "top: ".concat(a + 88, "rpx; bottom: 0") : "bottom: ".concat(wx.getSystemInfoSync().windowHeight - a - 1, "px; top: 0;"), 
                n.setData(_defineProperty({}, "searchList.".concat(r), t));
            });
        },
        onMobileFail: function(e) {
            var t = e.detail.value;
            this.$eventEmit("fail", {
                value: t
            });
        },
        onMobileInput: function(e) {
            var t = e.detail, a = t.name, n = t.value;
            this.$eventEmit("input", {
                name: a,
                value: n
            });
        },
        onMobileClear: function(e) {
            var t = e.detail.name;
            this.$eventEmit("clear", {
                name: t
            });
        },
        changeMobile: function() {
            wx.navigateTo({
                url: "/pages/changemobile/changemobile?mode=changeMobile"
            });
        },
        $eventEmit: function(e, t) {
            this.triggerEvent(e, t, {});
        }
    }
});